<?php

/**
 * SmartPHP framework
 *
 * @name SmartPHP framework
 * @package SmartPHP
 * @author 费尔 <admin@smartphp.cn>
 * @version 1.0 Beta
 * @link http://www.smartphp.cn
 * @copyright smartphp.cn
 * @license <a href="http://www.smartphp.cn">smartphp</a>
 */

namespace Base;

/**
 * 客户请求分析类
 *
 * @author 费尔 <admin@smartphp.cn>
 */
class Request {

    /**
     * 当前的请求类型
     * @access public
     * @return string
     */
    public static function method() {
        return self::isCli() ? 'GET' : strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
    }

    /**
     * 是否为GET请求
     * @access public
     * @return bool
     */
    public static function isGet() {
        return self::method() === 'GET';
    }

    /**
     * 是否为POST请求
     * @access public
     * @return bool
     */
    public static function isPost() {
        return self::method() === 'POST';
    }

    /**
     * 是否为PUT请求
     * @access public
     * @return bool
     */
    public static function isPut() {
        return self::method() === 'PUT';
    }

    /**
     * 是否为DELTE请求
     * @access public
     * @return bool
     */
    public static function isDelete() {
        return self::method() === 'DELETE';
    }

    /**
     * 是否为HEAD请求
     * @access public
     * @return bool
     */
    public static function isHead() {
        return self::method() === 'HEAD';
    }

    /**
     * 是否为PATCH请求
     * @access public
     * @return bool
     */
    public static function isPatch() {
        return self::method() === 'PATCH';
    }

    /**
     * 是否为OPTIONS请求
     * @access public
     * @return bool
     */
    public static function isOptions() {
        return self::method() === 'OPTIONS';
    }

    /**
     * 是否为cli
     * @access public
     * @return bool
     */
    public static function isCli() {
        return PHP_SAPI == 'cli' ? true : false;
    }

    /**
     * 是否为cgi
     * @access public
     * @return bool
     */
    public static function isCgi() {
        return strstr(PHP_SAPI, 'cgi') ? true : false;
    }

    /**
     * 是否为Windows系统
     * @access public
     * @return bool
     */
    public static function isWin() {
        return strstr(PHP_OS, 'WIN') ? true : false;
    }

    /**
     * 当前是否Ajax请求
     * @access public
     * @return bool
     */
    public static function isAjax() {
        return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false;
    }

    /**
     * 当前是否Pjax请求
     * @access public
     * @return bool
     */
    public static function isPjax() {
        return (isset($_SERVER['HTTP_X_PJAX']) && $_SERVER['HTTP_X_PJAX']) ? true : false;
    }

    /**
     * 检测是否使用手机访问
     * @access public
     * @return bool
     */
    public function isMobile() {
        return (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) || (strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) || (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) || (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false;
    }

    /**
     * 判断是否是通过微信访问
     *
     * @access public
     * @return boolean
     */
    public static function isWeixin() {
        return strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ? true : false;
    }

    /**
     * 判断是否是通过微信访问
     *
     * @access public
     * @return boolean
     */
    public static function isWeChat() {
        return self::isWeixin();
    }

    /**
     * 返回客户端IP
     * @access public
     * @param int $type
     * @return string
     */
    public static function ip($type = 0) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos)
                unset($arr[$pos]);
            $ip = trim($arr[0]);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']))
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        elseif (isset($_SERVER['REMOTE_ADDR']))
            $ip = $_SERVER['REMOTE_ADDR'];
        $long = sprintf("%u", ip2long($ip));
        $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
        return $ip[$type];
    }

    /**
     * 返回IP详情
     * @access public
     * @param string|NULL $ip
     * @return array|string
     */
    public static function ipInfo($ip = NULL) {
        return \Help\Ip::getInfo($ip ?: self::ip());
    }

    /**
     * 当前是否ssl
     * @access public
     * @return bool
     */
    public static function isSsl() {
        return (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) || (isset($_SERVER['REQUEST_SCHEME']) && 'https' == $_SERVER['REQUEST_SCHEME']) || (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO']) ? true : false;
    }

}
